package com.cham.pinglun.controller;

import com.cham.enums.ChamSysCodeEnum;
import com.cham.pinglun.dto.PinglunInfo;
import com.cham.pinglun.dto.PinglunSearchForm;
import com.cham.pinglun.entity.Pinglun;
import com.cham.pinglun.service.PinglunService;
import com.cham.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 请描述一下这个类文件吧
 * @createTime 2020年03月31日 15:45:00
 */
@Controller
@RequestMapping("/pinglunController")
public class PinglunController {

    @Autowired
    private PinglunService pinglunService;

    @PostMapping("/savePinglun")
    @ResponseBody
    public ChamSysCodeEnum savePinglun(@RequestBody Pinglun pinglun){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        pinglun.setUserUUid(userUUID);
        pinglun.setPinglunCreateTime(new Date());
        pinglunService.savePinglun(pinglun);

        return ChamSysCodeEnum.USER_REGISTER_SUCCESS;
    }

    @PostMapping("/getPinglunInfo")
    @ResponseBody
    public List<PinglunInfo> getPinglunInfo(@RequestBody PinglunSearchForm pinglunSearchForm){
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String token = request.getHeader("token");
//        String userUUID = TokenUtils.getLoginUserUUID(token);
//        pinglun.setUserUUid(userUUID);
//        pinglun.setPinglunCreateTime(new Date());
//        pinglunService.savePinglun(pinglun);
        return pinglunService.getPinglunInfo(pinglunSearchForm.getVideoId(),pinglunSearchForm.getStart(),pinglunSearchForm.getPageSize());
    }

    @GetMapping("/getPinglunInfoCount")
    @ResponseBody
    public int getPinglunInfoCount(int videoId){
        return pinglunService.getPinglunInfoCount(videoId);
    }

}
